﻿using Newtonsoft.Json;
using Super.Hmi.Global;
using System.IO;

namespace Super.Hmi.Utils.ThemeManager
{
    /// <summary>
    /// 主题设置
    /// </summary>
    public sealed class ThemeSettingHelper
    {
        /// <summary>
        /// 主题配置
        /// </summary>
        public ThemeConfig ThemeConfig { get; set; } = new ThemeConfig();
        public static readonly Lazy<ThemeSettingHelper> _instance = new Lazy<ThemeSettingHelper>(() => new ThemeSettingHelper());
        public static ThemeSettingHelper Instance => _instance.Value;
        private ThemeSettingHelper() { }
        /// <summary>
        /// 是否开始应用主题
        /// </summary>
        public bool IsApplyTheme { get; set; } = false;
        /// <summary>
        /// 切换白天和黑夜主题
        /// 由于HandyControl 很多主题是基于颜色来的，所以都需要重新加载
        /// </summary>
        /// <param name="_themetype">类型</param>
        public void SwitchThemeDrakOrLight(string _themetype)
        {
            if (string.IsNullOrEmpty(_themetype)) return;
            Enum.TryParse<ThemeType>(_themetype, out ThemeType themeType);
            ThemeConfig.AppearanceSettings.Theme.ThemeType = themeType;
        }
        /// <summary>
        /// 切换主题颜色 保存在数据库中
        /// </summary>
        /// <param name="color"></param>
        public void SwitchThemeColor(string color)
        {
            if (string.IsNullOrEmpty(color)) return;
            ThemeConfig.AppearanceSettings.BuiltInThemes.PrimaryColor = color;
        }
        /// <summary>
        /// 切换主题圆角
        /// </summary>
        /// <param name="cornerRadius"></param>
        public void SwitchThemeCornerRadius(double cornerRadius)
        {
            ThemeConfig.AppearanceSettings.CornerRadiusType.CornerRadius = cornerRadius;
        }
        /// <summary>
        /// 切换登录布局
        /// </summary>
        /// <param name="layout"></param>
        public void SwitchLoginLayout(string layout)
        {
            if (string.IsNullOrEmpty(layout)) return;
            LoginLayoutType loginType = LoginLayoutType.Left;
            bool res = Enum.TryParse<LoginLayoutType>(layout, out loginType);
            if (res)
            {
                ThemeConfig.LayoutSettings.LoginLayout.LoginLayoutType = loginType;
            }
        }
        /// <summary>
        /// 保存设置
        /// </summary>
        public void Save()
        {
            var json = JsonConvert.SerializeObject(ThemeConfig, Formatting.Indented);
            File.WriteAllText(FilePath.ThemeSettingFilePath, json);
        }
        /// <summary>
        /// 应用主题配置
        /// </summary>
        public void ApplyTheme()
        {
            IsApplyTheme = false;
            if (!File.Exists(FilePath.ThemeSettingFilePath))
            {
                JsonHelper.SaveObjectToJsonFile(new ThemeConfig(), FilePath.ThemeSettingFilePath);
            }
            var json = File.ReadAllText(FilePath.ThemeSettingFilePath);
            ThemeConfig = JsonHelper.GetObjectFromJsonString<ThemeConfig>(json);
            IsApplyTheme = true;
        }
        /// <summary>
        /// 切换语言 中英切换
        /// </summary>
        /// <param name="language"></param>
        public void SwitchLanguage(string language)
        {
            if (string.IsNullOrEmpty(language)) return;
            Enum.TryParse<LanguageType>(language, out LanguageType languageType);
            ThemeConfig.GeneralSettings.General.Language = languageType;
        }
        /// <summary>
        /// 切换主题布局
        /// </summary>
        /// <param name="mainLayout"></param>
        public void SwitchThemeMainLayout(string mainLayout)
        {
            if (string.IsNullOrEmpty(mainLayout)) return;
            NavigationPosition navigationPosition = NavigationPosition.HorizontalMenu;
            bool res = Enum.TryParse<NavigationPosition>(mainLayout, out navigationPosition);
            if (res)
            {
                ThemeConfig.LayoutSettings.Layout.NavigationPosition = navigationPosition;
            }
        }
    }
}
